package actions;

import java.util.ArrayList;

import javax.ejb.EJB;

import beans.UserManager;
import beans.UserManagerLocal;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DeleteFriendAction extends ActionSupport {

	@EJB
	private UserManagerLocal userMgr;
	private Integer[] isChecked;
	private Integer[] isAFChecked;

	private boolean antifriend = false;
	private boolean deleteFriend = false;
	private boolean deleteAntifriend = false;
	private boolean friend = false;

	public String execute() {
		userMgr = new UserManager();
		Integer userIdObj = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		if (userIdObj == null) {
			this.addActionError("UserID is null");
			return ERROR;
		}

		if (deleteFriend) {
			if (isChecked != null && isChecked.length == 1) {
				this.addActionMessage("No friend checked.");
			}
			ArrayList<Integer> failures = userMgr.deleteFriend(isChecked,
					userIdObj);
			if (!(failures.isEmpty())) {
				String message = "Deletion of following friend IDs failed: ";
				for (int i = 0; i < failures.size(); i++) {
					message += failures.get(i).intValue() + " ";
				}
				this.addActionError(message);
			}
		} else if (antifriend) {
			if (isChecked != null && isChecked.length == 1) {
				this.addActionMessage("No friend checked.");
			}
			ArrayList<Integer> failures = userMgr.makeAntifriend(isChecked,
					userIdObj);
			if (!(failures.isEmpty())) {
				String message = "The following friend still remain friends: ";
				for (int i = 0; i < failures.size(); i++) {
					message += failures.get(i).intValue() + " ";
				}
				this.addActionError(message);
			}
		} else if (deleteAntifriend) {
			if (isAFChecked != null && isAFChecked.length == 1) {
				this.addActionMessage("No friend checked.");
			}
			ArrayList<Integer> failures = userMgr.deleteAntifriend(isAFChecked,
					userIdObj);
			if (!(failures.isEmpty())) {
				String message = "The following friend still remain friends: ";
				for (int i = 0; i < failures.size(); i++) {
					message += failures.get(i).intValue() + " ";
				}
				this.addActionError(message);
			}
		} else if (friend) {
			if (isAFChecked != null && isAFChecked.length == 1) {
				this.addActionMessage("No antifriend checked.");
			}
			ArrayList<Integer> failures = userMgr.makeFriend(isAFChecked,
					userIdObj);
			if (!(failures.isEmpty())) {
				String message = "Failed IDs: ";
				for (int i = 0; i < failures.size(); i++) {
					message += failures.get(i).intValue() + " ";
				}
				this.addActionError(message);
			}
		}
		return SUCCESS;

	}

	public Integer[] getIsAFChecked() {
		return isAFChecked;
	}

	public void setIsAFChecked(Integer[] isAFChecked) {
		this.isAFChecked = isAFChecked;
	}

	public Integer[] getIsChecked() {
		return isChecked;
	}

	public void setIsChecked(Integer[] isChecked) {
		this.isChecked = isChecked;
	}

	public boolean isAntifriend() {
		return antifriend;
	}

	public void setAntifriend(boolean antifriend) {
		this.antifriend = true;
	}

	public boolean isDeleteFriend() {
		return deleteFriend;
	}

	public void setDeleteFriend(boolean deleteFriend) {
		this.deleteFriend = true;
	}

	public boolean isDeleteAntifriend() {
		return deleteAntifriend;
	}

	public void setDeleteAntifriend(boolean deleteAntifriend) {
		this.deleteAntifriend = true;
	}

	public boolean isFriend() {
		return friend;
	}

	public void setFriend(boolean friend) {
		this.friend = true;
	}

}
